﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// BusyIndicator.xaml 的交互逻辑
    /// </summary>
    public class BusyIndicator : ContentControl
    {
        static BusyIndicator()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BusyIndicator), new FrameworkPropertyMetadata(typeof(BusyIndicator)));
        }
        public BusyIndicator()
        {
            this.Loaded += BusyIndicator_Loaded;
        }

        private void BusyIndicator_Loaded(object sender, RoutedEventArgs e)
        {
            //this.Name = "busy";
            if (this.Parent is FrameworkElement fe)
            {
                System.Windows.NameScope ns = new NameScope();
                NameScope.SetNameScope(fe, ns);
                ns.RegisterName("busy", this);
                //为所有BusyIndicator注册名字“busy”
            }

        }

        public bool IsBusy
        {
            get { return (bool)GetValue(IsBusyProperty); }
            set { SetValue(IsBusyProperty, value); }
        }

        public static readonly DependencyProperty IsBusyProperty =
            DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyIndicator), new PropertyMetadata(false));


        /// <summary>
        /// 遮罩层画刷
        /// </summary>
        public Brush MaskBrush
        {
            get { return (Brush)GetValue(MaskBrushProperty); }
            set { SetValue(MaskBrushProperty, value); }
        }

        public static readonly DependencyProperty MaskBrushProperty =
            DependencyProperty.Register("MaskBrush", typeof(Brush), typeof(BusyIndicator), new PropertyMetadata(Brushes.Gray));


        /// <summary>
        /// 遮罩层透明度
        /// </summary>
        public double MaskOpacity
        {
            get { return (double)GetValue(MaskOpacityProperty); }
            set { SetValue(MaskOpacityProperty, value); }
        }

        public static readonly DependencyProperty MaskOpacityProperty =
            DependencyProperty.Register("MaskOpacity", typeof(double), typeof(BusyIndicator), new PropertyMetadata(0.5));



        public object BusyContent
        {
            get { return (object)GetValue(BusyContentProperty); }
            set { SetValue(BusyContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BusyContent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BusyContentProperty =
            DependencyProperty.Register("BusyContent", typeof(object), typeof(BusyIndicator), new PropertyMetadata(""));



        public Visibility MaskVisibility
        {
            get { return (Visibility)GetValue(MaskVisibilityProperty); }
            set { SetValue(MaskVisibilityProperty, value); }
        }

        public static readonly DependencyProperty MaskVisibilityProperty =
            DependencyProperty.Register("MaskVisibility", typeof(Visibility), typeof(BusyIndicator), new PropertyMetadata(Visibility.Collapsed));



        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == IsBusyProperty)
            {
                if ((bool)e.NewValue)
                {
                    MaskVisibility = Visibility.Visible;
                }
                else
                {
                    MaskVisibility = Visibility.Collapsed;
                }
            }
        }
    }
}
